import Vue from "vue";
import Vuex from "vuex";
import storage from "@/utils/storage";
import { API } from "@/config/myApi.js";
Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		// shareLink: "", //分享链接
		// verificationKey: "", //获取key表示验证通过
		// distributionId: "", //分销员Id 如果当前账户从未登录过时记录
		// hasLogin: storage.getHasLogin() || {},
		userInfo:storage.getUserInfo() || {}, //  
		uuid:storage.getUuid() ||  '',// 
		token: "",
		expertId: storage.getUserInfo() ? storage.getUserInfo().id :'',
		workbenchData:{},
		u_centerData:{},// storage.getU_Center() || 
		// isShowAthen:false, // 是否展示认证
		toastDuration:1000,
		rows:10,
	},
	mutations: {
		login(state, userInfo) {
			state.userInfo = userInfo || {};
			state.expertId = userInfo.id;
			// state.userName = userInfo.Name || userInfo.Nickname || userInfo.Username || "匿名用户";
			// state.hasLogin = true;
			console.log('专家id',userInfo,state.expertId);
		},
		loginOut(state) {
			// state.userName = "";
			// state.hasLogin = false;
			state.hasLogin = {};
			state.userInfo = {};
			state.uuid = '';
			state.token = "";
			state.expertId = '';
			state.workbenchData = {};
			state.u_centerData = {};
			console.log('清除仓库');
			// 待定
		},
		M_userInfo(state, val) {
			state.userInfo = {
				...val
			};
			state.expertId = val.id;
		},
		M_workbenchData(state,val) {
			state.workbenchData = {
				...val
			};
		},
		M_u_center(state,val) {
			state.u_centerData = {
				...val
			};
		}
	},
	actions: {
		async A_userInfo({commit}) {
			let res = await API.expertInfo();
			if (res.code === 200) {
				storage.setUserInfo(res.result)
				commit('M_userInfo', res.result)
				commit('login',res.result)
				console.log('修改了');
				
			}
			if(res.code == 20003) {
				uni.reLaunch({
					url:"/pages/passport/login"
				})
			}
			console.log("专家个人信息", res);
		},
		// 获取工作台信息
		async A_workbenchData({commit}) {
			let res = await API.workbench();
			if (res.code === 200) {
				commit('M_workbenchData', res.result)
				console.log("工作台", res);
				
			} else{
				uni.showToast({
					title:res.message,
					icon:"none"
				})
			}
		},
		// 个人中心专家用户信息
		A_u_center({commit}){
			API.u_center().then(res =>{
				if(res.code === 200){
					storage.setU_Center(res.result);
					commit ('M_u_center',res.result)
					// this.face = res.result.face;
					console.log('个人中心专家用户信息',res.result);
				}
			})
		},
		// 退出
		A_loginOut({commit}) {
			// API.userLogout().then(res => {
				// if (res.code === 200) {
					console.log('登出清空本地存储')
					storage.removeAccessToken();
					storage.removeUuid();
					storage.removeUserInfo();
					storage.removeRefreshToken();
					storage.removeExists();
					uni.removeStorageSync('knowledgeDetail')
					commit('loginOut')
					uni.reLaunch({
						url: "/pages/passport/login"
					})
				// }
			// })
		}
		
	},

});

export default store;
